package com.kingyea.app.market.userappmenu.service.impl;

import com.kingyea.app.market.userappmenu.bean.UserAppMenu;
import com.kingyea.app.market.userappmenu.dao.UserAppMenuMapper;
import com.kingyea.app.market.userappmenu.service.UserAppMenuService;
import com.kingyea.app.market.utils.StateCode;
import com.kingyea.mobilepolice.system.exception.BusinessException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author hx
 * @version v1.0.0
 * @description UserAppMenu 实现类
 * @date Created in 2018-04-24 11:42:24
 */
@Service
@Transactional
public class UserAppMenuServiceImpl implements UserAppMenuService {

    private static final Logger LOGGER = LoggerFactory.getLogger(UserAppMenuServiceImpl.class);

    @Resource
    private UserAppMenuMapper userAppMenuMapper;

    /**
     * 通过ID查找对象
     *
     * @param id
     * @return
     */
    @Override
    public UserAppMenu getById(Integer id) throws Exception {
        return this.userAppMenuMapper.getById(id);
    }

    /**
     * 列表
     *
     * @param
     * @return
     */
    @Override
    public List<UserAppMenu> list(UserAppMenu userAppMenu) throws Exception {
        List<UserAppMenu> list = userAppMenuMapper.list(userAppMenu);
        return list;
    }

    @Override
    public List<UserAppMenu> myApp(String userId) throws Exception {
        // 当前用户所有桌面
        List<Map> desktopList = getDesktop(userId);
        // 当前用户所有区域
        List list = new ArrayList();
        List<Map> regionList = getRegion(userId);
        UserAppMenu userappmenu = new UserAppMenu();
        userappmenu.setUserId(userId);
        List<UserAppMenu> userAppMenuList = userAppMenuMapper.list(userappmenu);
        //桌面
        for (Map desktopMap : desktopList) {
            if (desktopMap.get("desktop") != null) {
                Integer desktop = Integer.parseInt(desktopMap.get("desktop").toString());
                List _desktopList = new ArrayList();
//				Map<String, Object> _desktopMap = new HashMap<>();
                //区域
                for (Map regionMap : regionList) {
                    if (regionMap.get("region") != null) {
                        String region = regionMap.get("region").toString();
                        List _regionList = new ArrayList();
//						Map<String, Object> _regionMap = new HashMap<>();
                        //app
                        for (UserAppMenu userAppMenu : userAppMenuList) {
                            //同个桌面同个区域
                            if (desktop == userAppMenu.getDesktop() && region.equals(userAppMenu.getRegion())) {
                                _regionList.add(userAppMenu);
                            }
                        }
//						_regionMap.put(region,_regionList);
                        _desktopList.add(_regionList);
                    }
                }
//				_desktopMap.put(desktopMap.get("desktop").toString(),_desktopList);
                list.add(_desktopList);
            }
        }
        return list;
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @Override
    public int deleteById(Integer id) throws Exception {
        userAppMenuMapper.deleteById(id);
        return 1;
    }

    /**
     * 保存
     *
     * @param userAppMenu
     * @return
     */
    @Override
    public UserAppMenu save(UserAppMenu userAppMenu) throws Exception {
        userAppMenuMapper.save(userAppMenu);
        return userAppMenu;
    }

    /**
     * 更新
     *
     * @param userAppMenu
     * @return
     */
    @Override
    public UserAppMenu update(UserAppMenu userAppMenu) throws Exception {
        userAppMenuMapper.update(userAppMenu);
        return userAppMenu;
    }

    @Override
    public List<Map> getDesktop(String userId) throws Exception {
        return userAppMenuMapper.getDesktop(userId);
    }

    @Override
    public List<Map> getRegion(String userId) throws Exception {
        return userAppMenuMapper.getRegion(userId);
    }

    @Override
    public Object deletesByAppCode(String appCode) throws Exception {
        if (org.apache.commons.lang3.StringUtils.isBlank(appCode)) {
            throw new BusinessException(StateCode.NO_ARGUMENT_ERROR);
        }
        return userAppMenuMapper.deletesByAppCode(appCode);
    }


}
